package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.net.aso.l;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.FavoritesTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.SQLs;
import com.scpii.universal.util.SpringBeanFactory;
import com.sun.org.apache.xpath.internal.operations.And;

public class FavoritesTabService implements BaseInterface<FavoritesTab> {
	private static final Logger log = LoggerFactory
			.getLogger(FavoritesTabService.class);
//	private BaseDao<FavoritesTab> favoritesTabDao = SpringBeanFactory.favoritesTabDao;
	private BaseDao<FavoritesTab> favoritesTabDao;
	
	public FavoritesTab get(Integer id) throws Exception {
		return favoritesTabDao.get(id);
	}

	public List<FavoritesTab> getAll() throws Exception {
		return favoritesTabDao.getAll();
	}

	public FavoritesTab get(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public List<FavoritesTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(FavoritesTab obj) throws Exception {
		favoritesTabDao.save(obj);
	}

	public void update(FavoritesTab obj) throws Exception {

	}

	public void delete(FavoritesTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<FavoritesTab> hqlGetList(String hql,
			Map<String, Object> argsMap) throws Exception {
		return favoritesTabDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		favoritesTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return favoritesTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<FavoritesTab> sqlGetList(String sqls,
			Map<String, Object> argsMap) throws Exception {
		return favoritesTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		favoritesTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return favoritesTabDao.sqlGetNum(sqls, argsMap);
	}
	
	/**
	 * 验证内容是否重复收藏，用户只能对内容收藏一次。
	 * @param userId
	 * @param contentId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-16下午11:45:02
	 */
	public boolean checkSameFavoritesIn(int userId,int contentId)throws Exception{
		if (userId>0 && contentId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("contentId", contentId);
			argsMap.put("userId", userId);
			
			Long resultCount = this.hqlGetNums("CHECK_ADD_FAVORITES_COUNT", argsMap);
			if (resultCount==0) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 根据用户ID，和应用ID，获得收藏列表
	 * @param appId
	 * @param userId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-18下午05:28:18
	 */
	public List<FavoritesTab> getFavoritesListByUserId(int appId,int userId)throws Exception{
		if (appId>0) {
			List<FavoritesTab> favoritesList = new ArrayList<FavoritesTab>();
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			argsMap.put("userId", userId);
			
			favoritesList = this.hqlGetList("GET_FAVORITES_LIST_BY_USERID_APPID", argsMap);
			return favoritesList;
		}
		return null;
	}
	
	/**
	 * 获取收藏列表的页数
	 * @param appId
	 * @param userId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午09:48:21
	 */
	public Integer getFavoritesPageCount(int appId,int userId)throws Exception{
		if (appId>0 && userId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			argsMap.put("userId", userId);
			
			Long resultCount = this.hqlGetNums("GET_FAVORITES_LIST_PAGE_COUNT_BY_USERID_APPID", argsMap);
			int result = resultCount.intValue();
			int pageCount = favoritesTabDao.getRecordPageNum(result, Const.MOBILE_CLIENT_NO_PERPAGE);
			
			return pageCount;
		}
		return 0;
	}
	
	/**
	 * 分页获取用户收藏列表
	 * @param appId
	 * @param userId
	 * @param pageNum
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午09:57:24
	 */
	public List<FavoritesTab> getFavoritesListByPageNo(int appId,int userId,int pageNum)throws Exception{
		if (appId>0 && userId>0) {
			List<FavoritesTab> favoritesList = new ArrayList<FavoritesTab>();
			
			int MAXROWNUM = pageNum * Const.MOBILE_CLIENT_NO_PERPAGE;
			int MINROWNUM = (pageNum - 1) * Const.MOBILE_CLIENT_NO_PERPAGE;
			
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			argsMap.put("userId", userId);
			argsMap.put("MAXROWNUM", MAXROWNUM);
			argsMap.put("MINROWNUM", MINROWNUM);
			
			favoritesList = this.sqlGetList(SQLs.GET_FAVORITES_LIST_BY_PAGE, argsMap);
			
			return favoritesList;
		}
		return null;
	}

	//Setter
	public void setFavoritesTabDao(BaseDao<FavoritesTab> favoritesTabDao) {
		this.favoritesTabDao = favoritesTabDao;
	}
}